﻿using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class MaxBannerUnit : MonoBehaviour
{

	private string bannerAdUnitId;

	private string alias;
	public float retryTime = 3f;
	public Color backgroundColor = Color.black;
	public MaxSdkBase.BannerPosition bannerPos = MaxSdkBase.BannerPosition.BottomCenter;
	public Action<string, string, int> callback;
	// Use this for initialization
	public void Init(string adUnitId, string name)
	{
        
		bannerAdUnitId = adUnitId;
		alias = name;

		if (string.IsNullOrEmpty(adUnitId))
		{
			Debug.LogError(alias + " 横幅广告UnitId为空");
			return;
		}
		
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 

		if (MaxSdk.IsInitialized())
		{
			InitializeBannerAds();
		}
		else
		{
			MaxSdkCallbacks.OnSdkInitializedEvent += Initialize;
		}
#endif
	}
	
	void Initialize(MaxSdkBase.SdkConfiguration sdkConfiguration)
	{
		InitializeBannerAds();
	}

	public void ShowAds()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdk.ShowBanner(bannerAdUnitId);
#endif
	}

	public void HideAds()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdk.HideBanner(bannerAdUnitId);
#endif
	}

	public void DestroyBanner()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdk.DestroyBanner(bannerAdUnitId);
#endif
	}


	private void OnDestroy()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdkCallbacks.OnSdkInitializedEvent -= Initialize;
#endif
		DestroyBanner();
	}

	public void InitializeBannerAds()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		// Banners are automatically sized to 320x50 on phones and 728x90 on tablets
		// You may use the utility method `MaxSdkUtils.isTablet()` to help with view sizing adjustments
		MaxSdk.CreateBanner(bannerAdUnitId, bannerPos);
		// Set background or background color for banners to be fully functional
		MaxSdk.SetBannerBackgroundColor(bannerAdUnitId, backgroundColor);
#endif
	}
	
}
